package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.acticle.domain.AswerDetail;
import cn.wolfcode.luowowo.acticle.domain.StrategyDetail;
import cn.wolfcode.luowowo.acticle.service.IAswerService;
import cn.wolfcode.luowowo.acticle.service.IStrategyDetailService;
import cn.wolfcode.luowowo.acticle.vo.AswerStatisVo;
import cn.wolfcode.luowowo.acticle.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.prefix.AswerKeyPrefix;
import cn.wolfcode.luowowo.cache.prefix.StrategyStatisKeyPrefix;
import cn.wolfcode.luowowo.cache.service.IRedisService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * redis数据初始化监听器, 在容器启动时完成初始化, 在mgrsite项目中部署该监听器(website可能会集群, 需要部署多个监听器)
 */
@Component //1.将定时器交给springboot容器管理     2.实现该接口
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IRedisService redisService;
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    @Reference
    private IAswerService aswerService;

    //容器开启时马上执行的方法
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        //获取mysql中所有的攻略明细数据, 遍历集合再判断redis中是否存在该数据, 不存在是设置到vo中, 然后保存到redis中
        List<StrategyDetail> details = strategyDetailService.list();
        for (StrategyDetail detail : details) {
            if (!redisService.exists(StrategyStatisKeyPrefix.STRATEGY_STATIS_VO, detail.getId() + "")) {
                StrategyStatisVO vo = new StrategyStatisVO();
                try {
                    BeanUtils.copyProperties(vo, detail);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                vo.setDestName(detail.getDest().getName());
                vo.setDestId(detail.getDest().getId());
                vo.setStrategyId(detail.getId());
                redisService.set(StrategyStatisKeyPrefix.STRATEGY_STATIS_VO, detail.getId() + "", vo);
            }
        }
        System.out.println("================完成vo的初始化================");
        for (StrategyDetail detail : details) {
            //vo中收藏数 + 点赞数 = 20
            //strategy_statis_commend_sort   205   strategy_statis_vo:sid
            if (!redisService.hasZsetValue(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_SORT, "",
                    StrategyStatisKeyPrefix.STRATEGY_STATIS_VO.getPrefix() + ":" + detail.getId())) {
                int increment = detail.getFavornum() + detail.getThumbsupnum();
                strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_SORT, detail.getId(), increment);
            }
        }
        System.out.println("================完成国内/国外的攻略推荐数据初始化================");
        for (StrategyDetail detail : details) {
            if (!redisService.hasZsetValue(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_HOT, "",
                    StrategyStatisKeyPrefix.STRATEGY_STATIS_VO.getPrefix() + ":" + detail.getId())) {
                int increment = detail.getViewnum() + detail.getReplynum();
                strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_HOT, detail.getId(), increment);
            }
        }
        System.out.println("================完成热门的攻略推荐数据初始化================");


        List<AswerDetail> aswerDetails=aswerService.list();
        for (AswerDetail aswerDetail : aswerDetails) {
            if (!redisService.exists(AswerKeyPrefix.STATIC_TYPE_ASWVO, aswerDetail.getUserInfo().getId() + "")) {
                AswerStatisVo aswerStatisVo = new AswerStatisVo();
                try {
                    BeanUtils.copyProperties(aswerStatisVo, aswerDetail);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                aswerStatisVo.setUserInfoId(aswerDetail.getUserInfo().getId());
                redisService.set(AswerKeyPrefix.STATIC_TYPE_ASWVO, aswerDetail.getUserInfo().getId() + "", aswerStatisVo);
            }
        }
        System.out.println("================完成社区回答数据初始化================");

    }


    //容器关闭前将redis的数据持久化到mysql中
    public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
        List<StrategyStatisVO> vos = strategyStatisRedisService.getStatisVos();
        for (StrategyStatisVO vo : vos) {
            strategyDetailService.updateStatisData(vo);
        }
    }
}
